package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizAmpLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizAmpLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizAmpLogParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizAmpLogResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizAmpLogService;
import cn.stylefeng.guns.modular.gridsystem.bizamplog.enums.BizAmpLogExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 振动数据上报日志 服务实现类
 *
 * @author ssy
 * @date 2023/02/03 09:53
 */
@Service
public class BizAmpLogServiceImpl extends ServiceImpl<BizAmpLogMapper, BizAmpLog> implements BizAmpLogService {

    @Override
    public PageResult<BizAmpLogResult> page(BizAmpLogParam bizAmpLogParam) {
        String[] list = new String[1];
        String gatewayIds = bizAmpLogParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            list = gatewayIds.split(",");
        }else {
            list[0] = "";
        }
        // 查询分页结果
        return new PageResult<>(this.baseMapper.page(PageFactory.defaultPage(), bizAmpLogParam,list));
    }

    @Override
    public List<BizAmpLog> list(BizAmpLogParam bizAmpLogParam) {

        // 构造条件
        LambdaQueryWrapper<BizAmpLog> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(BizAmpLogParam bizAmpLogParam) {

        // 将dto转为实体
        BizAmpLog bizAmpLog = new BizAmpLog();
        BeanUtil.copyProperties(bizAmpLogParam, bizAmpLog);

        this.save(bizAmpLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizAmpLogParam bizAmpLogParam) {
        this.removeById(bizAmpLogParam.getId());
    }

    @Override
    public void edit(BizAmpLogParam bizAmpLogParam) {

        // 根据id查询实体
        BizAmpLog bizAmpLog = this.queryBizAmpLog(bizAmpLogParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizAmpLogParam, bizAmpLog);

        this.updateById(bizAmpLog);
    }

    @Override
    public BizAmpLog detail(BizAmpLogParam bizAmpLogParam) {
        return this.queryBizAmpLog(bizAmpLogParam);
    }



    /**
     * 获取振动数据上报日志
     *
     * @author ssy
     * @since 2023/02/03 09:53
     */
    private BizAmpLog queryBizAmpLog(BizAmpLogParam bizAmpLogParam) {
        BizAmpLog bizAmpLog = this.getById(bizAmpLogParam.getId());
        if (ObjectUtil.isEmpty(bizAmpLog)) {
            throw new ServiceException(BizAmpLogExceptionEnum.NOT_EXISTED);
        }
        return bizAmpLog;
    }
}
